<?php

/**
 * display.php
 * @package adServer
 * @filesource
 */

/*	Close the backdoor	*/
$curPage = strtoupper($_SERVER['PHP_SELF']);
if (isset($curPage) && strpos($curPage, 'INDEX.PHP') === false)
    header('Location:./index.php');

require_once (CLASS_PATH . 'template.class.php');

/* Load the side menu */
$sideMenu = file_get_contents(TEMPLATE_PATH . 'sideMenu.tpl');

/*	load the page header	*/
$header = new template(TEMPLATE_PATH . 'header.tpl');
$header->replace('&&SIDEMENU&&', $sideMenu);
$header->replace('&&STYLE&&', $session->loggedIn ? "style='display:block;'" : "");
$header->replace('&&USER&&', $session->username);

/*	Load the page content	*/
$pageContent = new template(TEMPLATE_PATH . $nextAction . '.tpl');

$pageContent->replace('&&MODNAME&&', 'Manage AdUnit');

$results = $session->getAdUnits();

if ($results != false) {
    $pageContent->replace('&&ADTABLE&&', array2table($results, 700));
} else {
    $pageContent->replace('&&ADTABLE&&',
        '<span class="success">The Ad Collection is empty</span>');
}


/*	Echo the page	*/
echo $header->templateHtml;
echo $pageContent->templateHtml;


function addViewHtmlLink($adId) {
    $thickbox = '&keepThis=true&TB_iframe=true&height=500&width=800';
    return "<a href='index.php?action=viewad&nofooter=true&adUnitId=$adId$thickbox' class='thickbox'>View</a>";
}

function addEditLink($adId) {
    return "<a href='index.php?action=editad&adUnitId=$adId$thickbox'>Edit</a>";
}

function addDeleteLink($adId) {
    $thickbox = '&height=500&width=800';
    return "<a href='index.php?action=deletead&nofooter=true&adUnitId=$adId$thickbox' class='thickbox'>Delete</a>";
}

function array2table($arr, $width) {
    $return = '';

    $count = count($arr);
    if ($count > 0) {
        reset($arr);
        $num = count(current($arr));
        $return .= "<table align=\"center\" border=\"1\"cellpadding=\"5\" cellspacing=\"0\" width=\"$width\">\n";
        $return .= "<tr>\n";
        foreach (current($arr) as $key => $value) {
            $return .= "<th>";
            $return .= $key . "&nbsp;";
            $return .= "</th>\n";
        }
        $return .= "<th>Html</th>\n";
        $return .= "<th>Edit</th>\n";
        $return .= "<th>Delete</th>\n";
        $return .= "</tr>\n";

        while ($curr_row = current($arr)) {
            $return .= "<tr>\n";
            $col = 1;
            while (false !== ($curr_field = current($curr_row))) {
                $return .= "<td>";
                if ($curr_field == '--')
                    $curr_field = 'All';
                $return .= $curr_field . "&nbsp;";
                $return .= "</td>\n";
                next($curr_row);
                $col++;
            }
            $return .= "<td>" . addViewHtmlLink($curr_row['adUnitId']) . "</td>";
            $return .= "<td>" . addEditLink($curr_row['adUnitId']) . "</td>";
            $return .= "<td>" . addDeleteLink($curr_row['adUnitId']) . "</td>";
            while ($col <= $num) {
                $return .= "<td>&nbsp;</td>\n";
                $col++;
            }
            $return .= "</tr>\n";
            next($arr);
        }
        $return .= "</table>\n";
    }
    return $return;
}

?>